package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 使用预编译SQL完成UPDATE操作
 */
public class JDBCDemo10 {
    public static void main(String[] args) {
        //需求：根据用户id修改用户密码，昵称，年龄

        try ( Connection  conn = DBUtil.getConnection();){
           String sql = "UPDATE userinfo SET password=?,nickname=?,age=? WHERE id=?";
           PreparedStatement ps = conn.prepareStatement(sql);
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入需要修改的用户id");
            ps.setInt(4,scan.nextInt());
            UserInfo userInfo = InputUtil.getInputObject(new UserInfo(),"请输入用户id,密码，昵称，年龄");
            ps.setString(1, userInfo.getPassword());
            ps.setString(2, userInfo.getNickName());
            ps.setInt(3,userInfo.getAge());

            int num = ps.executeUpdate();
            System.out.println(num>0?"修改成功":"修改失败");

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
